package com.huxiaohui.ssbx.utils;

import cn.hutool.core.util.ObjectUtil;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

import java.time.Duration;
import java.util.concurrent.CompletableFuture;

public class ResponseFutureHolder {
    private static final Cache<String, CompletableFuture<String>> sessionHandler = Caffeine.newBuilder()
            .expireAfterWrite(Duration.ofMinutes(5))
            .build();

    public static boolean put(String requestId, CompletableFuture<String> future) {
        if (ObjectUtil.isEmpty(requestId) || ObjectUtil.isNull(future)) {
            return false;
        }
        sessionHandler.put(requestId, future);
        return true;
    }

    public static CompletableFuture<String> get(String requestId) {
        if (ObjectUtil.isEmpty(requestId)) {
            return null;
        }
        return sessionHandler.getIfPresent(requestId);
    }


    public static void remove(String requestId) {
        if (ObjectUtil.isEmpty(requestId)) {
            return;
        }
        sessionHandler.invalidate(requestId);
    }
}
